<template>
  <div class="box">
    <div class="searcharea">
      <!-- 搜索 -->
      <el-row>
        <el-col :span="24">
          <div class="query">
            <el-form :inline="true" :model="queryMeasuringUnitParam" class="demo-form-inline">
              <el-form-item label="计量单位名称">
                <el-input
                  v-model="queryMeasuringUnitParam.MeasuringUnitName"
                  placeholder="请输入"
                  clearable
                  style="width: 200px"
                />
              </el-form-item>

              <el-form-item>
                <el-button type="primary" :icon="Search" @click="SearchMeasuringUnit()">搜索</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="contentarea">
      <!-- 功能按钮 -->
      <el-row>
        <el-col :span="24">
          <div class="functionbutton">
            <el-button type="primary" plain @click="addMeasuringUnit()">添加</el-button>
            <el-button type="danger" plain @click="batchDeletionMeasuringUnit()">批量删除</el-button>
          </div>
        </el-col>
        <el-dialog v-model="centerDialogVisible" title="添加计量单位" width="30%" align-center>
          <el-form
            ref="CreateMeasuringUnitForm"
            :model="CreateMeasuringUnitForm"
            :rules="CreateMeasuringUnitRules"
            label-width="auto"
            style="padding: 20px"
          >
            <el-form-item label="计量单位编号">
              <el-input v-model="CreateMeasuringUnitForm.MeasuringUnitNo" placeholder="不输入则自动生成"> </el-input>
            </el-form-item>
            <el-form-item prop="MeasuringUnitName" label="计量单位名称">
              <el-input v-model="CreateMeasuringUnitForm.MeasuringUnitName" placeholder="请输入计量单位名称">
              </el-input>
            </el-form-item>
          </el-form>

          <template #footer>
            <div class="dialog-footer">
              <el-button @click="centerDialogVisible = false">取消</el-button>
              <el-button type="primary" @click="CreateMeasuringUnitDialog()">确认</el-button>
            </div>
          </template>
        </el-dialog>
      </el-row>

      <!-- 数据表格 -->
      <el-row>
        <el-col :span="24">
          <div>
            <el-table
              :data="tableData"
              border
              style="width: 100%"
              max-height="400px"
              :selection="selectedRows"
              @selection-change="handleSelectionChange"
              @row-click="getRowkey"
              show-overflow-tooltip
              empty-text="暂无数据"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column type="index" label="序号" />
              <el-table-column prop="measuringUnitNo" label="计量单位编码" width="180" />
              <el-table-column prop="measuringUnitName" label="计量单位名称" />
              <el-table-column prop="creator" label="创建人" />
              <el-table-column prop="updator" label="修改人" />
              <el-table-column prop="createtime" label="创建时间" width="180" :formatter="formatColumnTime" />
              <el-table-column prop="updateDate" label="更新时间" width="180" :formatter="formatColumnTime" />
              <el-table-column fixed="right" label="操作" width="140">
                <template #default="scope">
                  <el-button type="danger" plain size="small" @click="DeleteMeasuringUnitSingle(scope.row.id)">
                    删除
                  </el-button>

                  <el-button type="primary" plain size="small" @click="UpdateMeasuringUnit(scope.row)">更新</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>

        <el-dialog v-model="UpdateDialogVisible" title="修改计量单位" width="30%" align-center>
          <el-form
            ref="UpdateMeasuringUnitForm"
            :model="UpdateMeasuringUnitForm"
            :rules="CreateMeasuringUnitRules"
            label-width="auto"
            style="padding: 20px"
          >
            <el-form-item label="计量单位编号">
              <el-input v-model="UpdateMeasuringUnitForm.MeasuringUnitNo" disabled placeholder="不输入则自动生成">
              </el-input>
            </el-form-item>
            <el-form-item prop="MeasuringUnitName" label="计量单位名称">
              <el-input v-model="UpdateMeasuringUnitForm.MeasuringUnitName" placeholder="请输入计量单位名称">
              </el-input>
            </el-form-item>
          </el-form>

          <template #footer>
            <div class="dialog-footer">
              <el-button @click="UpdateDialogVisible = false">取消</el-button>
              <el-button type="primary" @click="UpdateMeasuringUnitDialog()">确认</el-button>
            </div>
          </template>
        </el-dialog>
      </el-row>

      <!-- 分页 -->
      <el-row>
        <el-col :span="24">
          <div class="demo-pagination-block" style="display: flex; justify-content: flex-end">
            <el-config-provider :locale="zhCn">
              <el-pagination
                v-model:current-page="queryMeasuringUnitParam.PageIndex"
                v-model:page-size="queryMeasuringUnitParam.PageSize"
                :page-sizes="[100, 200, 300, 400]"
                :small="false"
                :disabled="false"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="queryMeasuringUnitParam.totalCount"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              >
              </el-pagination>
            </el-config-provider>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script >
import { Search } from '@element-plus/icons-vue'
import {
  ApiGetBasicData_MeasuringUnitPageListAsync,
  ApiBatchDeletionBasicData_MeasuringUnitAsync,
  ApiCreateBasicData_MeasuringUnitAsync,
  ApiUpdateBasicData_MeasuringUnitAsync
} from '@/api/basic/basicDataMeasuringUnitApi'
import { formatTime, capitalizeFirstLetter } from '../../utils/index'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { ElMessageBox, ElNotification } from 'element-plus'

export default {
  name: 'MeasuringUnitView',
  data() {
    return {
      UpdateDialogVisible: false,
      UpdateMeasuringUnitForm: {},
      CreateMeasuringUnitForm: {
        MeasuringUnitNo: null,
        MeasuringUnitName: null
      },
      CreateMeasuringUnitRules: {
        MeasuringUnitName: [{ required: true, trigger: 'blur', message: '计量单位名称不能为空' }]
      },
      ruleForm: {
        name: ''
      },
      centerDialogVisible: false,
      selectedRows: [], // 存储选中的行数据
      queryMeasuringUnitParam: {
        GoodsReceipttimeStart: null,
        GoodsReceipttimeEnd: null,
        MeasuringUnitName: null,
        PageIndex: 1,
        PageSize: 100,
        totalCount: 11
      },
      tableData: [] // 表格数据
    }
  },
  setup() {
    return {
      Search,
      zhCn
    }
  },
  created() {
    this.SearchMeasuringUnit()
  },
  methods: {
    //确认更新
    UpdateMeasuringUnitDialog() {
      this.$refs.UpdateMeasuringUnitForm.validate((valid) => {
        if (valid) {
          ApiUpdateBasicData_MeasuringUnitAsync(this.UpdateMeasuringUnitForm)
            .then((res) => {
              if (res.data.data) {
                ElNotification({
                  message: '更新成功',
                  offset: 100,
                  type: 'success',
                  duration: 2000
                })
                this.UpdateDialogVisible = false
                this.SearchMeasuringUnit()
              }
            })
            .catch(() => {
              ElNotification({
                message: '更新失败',
                type: 'warning',
                offset: 100,
                duration: 2000
              })
            })
        }
      })
    },
    //更新计量单位
    UpdateMeasuringUnit(res) {
      this.UpdateDialogVisible = true
      this.UpdateMeasuringUnitForm = capitalizeFirstLetter(res)
    },
    //删除计量单位
    DeleteMeasuringUnitSingle(res) {
      this.selectedRows = []
      this.selectedRows.push(res)
      this.batchDeletionMeasuringUnit()
    },
    //添加计量单位
    CreateMeasuringUnitDialog() {
      this.$refs.CreateMeasuringUnitForm.validate((valid) => {
        if (valid) {
          ApiCreateBasicData_MeasuringUnitAsync(this.CreateMeasuringUnitForm)
            .then((res) => {
              if (res.data.data) {
                ElNotification({
                  message: '添加计量单位成功',
                  offset: 100,
                  type: 'success',
                  duration: 2000
                })
                this.centerDialogVisible = false
                this.SearchMeasuringUnit()
              }
            })
            .catch(() => {
              ElNotification({
                message: '添加失败',
                type: 'warning',
                offset: 100,
                duration: 2000
              })
            })
        }
      })
    },

    //获取用户选择项
    handleSelectionChange(select) {
      // 提取传递过来的对象中的"id"字段，并存储到this.selectedRows中
      this.selectedRows = select.map((item) => item.id)
    },
    //添加计量单位
    addMeasuringUnit() {
      this.CreateMeasuringUnitForm.MeasuringUnitName = null
      this.CreateMeasuringUnitForm.MeasuringUnitNo = null
      this.centerDialogVisible = true
    },
    //指删除计量单位
    batchDeletionMeasuringUnit() {
      if (this.selectedRows.length < 1) {
        ElNotification({
          message: '请先选择一条或更多信息',
          type: 'warning',
          offset: 100,
          duration: 2000
        })
        return
      }
      ElMessageBox.confirm('确实要删除这些信息吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          ApiBatchDeletionBasicData_MeasuringUnitAsync(this.selectedRows)
            .then((res) => {
              if (res.data.data) {
                ElNotification({
                  message: '删除成功',
                  offset: 100,
                  type: 'success',
                  duration: 2000
                })
                this.selectedRows = []
                this.SearchMeasuringUnit()
              }
            })
            .catch(() => {
              this.selectedRows = []
            })
        })
        .catch(() => {
          this.selectedRows = []
        })
    },
    //时间转换
    formatColumnTime(row, column, time) {
      return formatTime(time)
    },

    //每页条数
    handleSizeChange(e) {
      this.queryMeasuringUnitParam.PageSize = e
      this.SearchMeasuringUnit()
    },

    // 搜索
    SearchMeasuringUnit() {
      ApiGetBasicData_MeasuringUnitPageListAsync(this.queryMeasuringUnitParam)
        .then((res) => {
          this.tableData = res.data.data
          this.queryMeasuringUnitParam.PageIndex = res.data.pageIndex
          this.queryMeasuringUnitParam.PageSize = res.data.pageSize
          this.queryMeasuringUnitParam.totalCount = res.data.totalCount
        })
        .catch((error) => {
          console.log('请求失败：', error)
        })
    }
  }
}
</script>
<style lang="less"  scoped>
@import '../../assets/globle/index.css';
</style>